home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 3 of 3
/
CHAPTE20
/
READRIFF.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-29
|
10KB
|
356 lines
#include <windows.h>
#include <stdio.h>
#include "ReadRiff.h"
#if defined (WIN32)
#define IS_WIN32 TRUE
#else
#define IS_WIN32 FALSE
#endif
#define IS_NT IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95 (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
HINSTANCE hInst; // current instance
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle = "My Application";
// the rest of the stuff
//......................
BOOL RegisterWin95( CONST WNDCLASS* lpwc );
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, lpszAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = lpszAppName;
wc.lpszClassName = lpszAppName;
if ( IS_WIN95 )
{
if ( !RegisterWin95( &wc ) )
return( FALSE );
}
else if ( !RegisterClass( &wc ) )
return( FALSE );
hInst = hInstance;
hWnd = CreateWindow( lpszAppName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hInstance,
NULL
);
if ( !hWnd )
return( FALSE );
ShowWindow( hWnd, nCmdShow );
UpdateWindow( hWnd );
while( GetMessage( &msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return( msg.wParam );
}
BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
WNDCLASSEX wcex;
wcex.style = lpwc->style;
wcex.lpfnWndProc = lpwc->lpfnWndProc;
wcex.cbClsExtra = lpwc->cbClsExtra;
wcex.cbWndExtra = lpwc->cbWndExtra;
wcex.hInstance = lpwc->hInstance;
wcex.hIcon = lpwc->hIcon;
wcex.hCursor = lpwc->hCursor;
wcex.hbrBackground = lpwc->hbrBackground;
wcex.lpszMenuName = lpwc->lpszMenuName;
wcex.lpszClassName = lpwc->lpszClassName;
// Added elements for Windows 95.
//...............................
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR );
return RegisterClassEx( &wcex );
}
#define MSG_LEN 1024
char msg[MSG_LEN+1];
VOID ReadRiff();
VOID PlayWave(LPSTR pFmt, LPSTR pData, LONG nDataSize);
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_TEST:
ReadRiff();
break;
case IDM_ABOUT :
DialogBox( hInst, "AboutBox", hWnd, About );
break;
case IDM_EXIT :
DestroyWindow( hWnd );
break;
}
break;
case WM_DESTROY :
PostQuitMessage(0);
break;
default :
return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return( 0L );
}
VOID ReadRiff()
{
HMMIO hmmio; // file handle for open file
MMCKINFO ciParentChunk; // parent chunk information
MMCKINFO ciSubChunk; // subchunk information structure
LONG nFmtSize; // size of "FMT" chunk
LONG nDataSize; // size of "DATA" chunk
LPSTR pFmt; // address of "FMT" chunk
LPSTR pData; // address of "DATA" chunk
// open the wave file using an internal I/O buffer
//................................................
hmmio = mmioOpen("Sample2.wav", NULL, MMIO_READ | MMIO_ALLOCBUF);
if(!hmmio)
{
MessageBox(NULL, "Failed to open file.", NULL, MB_OK);
return;
}
// locate the WAVE chunk within the RIFF chunk
//............................................
mmioSeek(hmmio, 0, SEEK_SET ); // reset to beginning of file.
ciParentChunk.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hmmio, (LPMMCKINFO) &ciParentChunk, NULL, MMIO_FINDRIFF))
{
MessageBox(NULL, "Not a waveform-audio file.", NULL, MB_OK);
mmioClose(hmmio, 0);
return;
}
// every waveform audio containes a 'fmt' and 'data' chunk,
// let's get the fmt chunk
//.........................................................
ciSubChunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hmmio, &ciSubChunk, &ciParentChunk, MMIO_FINDCHUNK))
{
MessageBox(NULL, "Unable to locate 'fmt' chunk.", NULL, MB_OK);
mmioClose(hmmio, 0);
return;
}
// allocate a buffer to hold the 'fmt' chunk
//..........................................
nFmtSize = ciSubChunk.cksize;
pFmt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nFmtSize);
// read the 'fmt' chunk
//.....................
if (mmioRead(hmmio, (HPSTR)pFmt, nFmtSize) != nFmtSize)
{
MessageBox(NULL, "Error reading 'fmt' chunk.", NULL, MB_OK);
HeapFree(GetProcessHeap(), 0, pFmt);
mmioClose(hmmio, 0);
return;
}
// ascend out of the 'fmt' subchunk
//.................................
mmioAscend(hmmio, &ciSubChunk, 0);
// descend into the 'data' chunk
//..............................
ciSubChunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hmmio, &ciSubChunk, &ciParentChunk, MMIO_FINDCHUNK))
{
MessageBox(NULL, "Unable to locate 'data' chunk.", NULL, MB_OK);
HeapFree(GetProcessHeap(), 0, pFmt);
mmioClose(hmmio, 0);
return;
}
// allocate a buffer to hold the 'data' chunk
//...........................................
nDataSize = ciSubChunk.cksize;
pData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nDataSize);
// Read the 'data' chunk
//......................
if(mmioRead(hmmio, (HPSTR)pData, nDataSize) != nDataSize)
{
MessageBox(NULL, "Error reading 'data' chunk.", NULL, MB_OK);
HeapFree(GetProcessHeap(), 0, pFmt);
HeapFree(GetProcessHeap(), 0, pData);
mmioClose(hmmio, 0);
return;
}
// Close the RIFF file
//....................
mmioClose(hmmio, 0);
// play the waveform-audio
//........................
PlayWave(pFmt, pData, nDataSize);
// free 'fmt' and 'data' buffers
//..............................
HeapFree(GetProcessHeap(), 0, pFmt);
HeapFree(GetProcessHeap(), 0, pData);
}
VOID PlayWave(LPSTR pFmt, LPSTR pData, LONG nDataSize)
{
// for more information on the usage of the
// waveOutOpen(), waveOutPrepareHeader(), waveOutWrite(),
// waveOutUnprepareHeader(), and waveOutClose() functions
// used in this example, see the chapter on waveform-audio
//........................................................
HWAVEOUT hwo;
LPWAVEHDR pWaveHdr;
MMRESULT rc;
// open waveform-audio driver
//...........................
rc = waveOutOpen(&hwo, 0, (LPWAVEFORMATEX)pFmt, (DWORD)NULL, 0L,
WAVE_ALLOWSYNC);
if (rc != MMSYSERR_NOERROR)
{
MessageBox(NULL, "Unable to open waveform-audio driver.", NULL, MB_OK);
return;
}
// allocate and configure WAVEHDR
//...............................
pWaveHdr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WAVEHDR));
if (!pWaveHdr)
{
MessageBox(NULL, "Unable to allocate WAVEHDR.", NULL, MB_OK);
return;
}
pWaveHdr->lpData = pData;
pWaveHdr->dwBufferLength = nDataSize;
pWaveHdr->dwFlags = 0L;
pWaveHdr->dwLoops = 0L;
// prepare, write, and unprepre WAVEHDR
//.....................................
waveOutPrepareHeader(hwo, pWaveHdr, sizeof(WAVEHDR));
waveOutWrite(hwo, pWaveHdr, sizeof(WAVEHDR));
waveOutUnprepareHeader(hwo, pWaveHdr, sizeof(WAVEHDR));
// free WAVEHDR buffer and close waveform output device
//.....................................................
HeapFree(GetProcessHeap(), 0, pWaveHdr);
waveOutClose(hwo);
}
LRESULT CALLBACK About( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if ( LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}